package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.annotation.AutoFill;
import com.sky.constant.MessageConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.enumeration.OperationType;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetMealDishMapper setMealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        //TODO 这里未返回套餐分类,使用一对多关联查询返回List<setmealDish>
        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);
        long total = page.getTotal();
        List<SetmealVO> result = page.getResult();
        return new PageResult(total, result);

    }

    @Override
    @Transactional
    @AutoFill(value = OperationType.INSERT)
    @CacheEvict(cacheNames = {"setmealCache", "setmealDishCache"})
    public void save(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.insert(setmeal);
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null && setmealDishes.size() > 0) {
            setmealDishes.forEach(item -> {
                item.setSetmealId(setmeal.getId());
            });

            setMealDishMapper.insertBatch(setmealDishes);
        }
    }

    @Override
    @Cacheable(value = "setmealCache", key = "#setmeal.categoryId")
    public List<Setmeal> setmealByCategoryId(Setmeal setmeal) {
        List<Setmeal> setmealList = setmealMapper.getSetmealByCategoryId(setmeal);
        return setmealList;
    }

    @Override
    @Cacheable(value = "setmealDishCache", key = "#id")
    public List<DishItemVO> setmealDishListBySetmealId(Long id) {
        List<DishItemVO> dishItemVOList = setmealMapper.getSetmealDishListBySetmealId(id);
        return dishItemVOList;
    }

    @CacheEvict(cacheNames = {"setmealCache"}, key = "#id")
    @Override
    public void startOrStop(Integer status, Long id) {
        //起售判断dish是否全部起售
        if (status == 1) {
            List<Dish> dishList = dishMapper.getDishListBySetmealId(id);
            boolean flag = true;
            for (Dish dish : dishList) {
                if (dish.getStatus() == 0) {
                    flag = false;
                    break; // 一旦找到一个状态为0的菜品，立即退出循环
                }
            }
            if(!flag){
                throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
           }

        }
        Setmeal build = Setmeal.builder().status(status).id(id).build();
        setmealMapper.update(build);
    }

    @Override
    public SetmealVO getSetmealAndSetmealDishById(Long id) {
        SetmealVO setmealVO = setmealMapper.getSetmealAndSetmealDishById(id);
        return setmealVO;
    }

    @CacheEvict(cacheNames = {"setmealCache", "setmealDishCache"})
    @Transactional
    @Override
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmeal.setStatus(0);
        setmealMapper.update(setmeal);
        //删除套餐菜品关系数据
        setMealDishMapper.deleteBySetmealId(setmeal.getId());
        //添加新的套餐菜品关系数据
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null && setmealDishes.size() > 0) {
            setmealDishes.forEach(item -> {
                item.setSetmealId(setmeal.getId());
            });
            setMealDishMapper.insertBatch(setmealDishes);
        }
    }

    @CacheEvict(cacheNames = {"setmealCache", "setmealDishCache"})
    @Transactional
    @Override
    public void deleteByIds(List<Long> ids) {
        setmealMapper.deleteBatchBySetmealId(ids);
        ids.forEach(i -> {
            setMealDishMapper.deleteBySetmealId(i);
        });

    }


}
